package br.elsa.survey.view.beans.managed;

import javax.annotation.PostConstruct;
import javax.enterprise.context.ConversationScoped;
import javax.enterprise.inject.New;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;

import br.elsa.survey.domain.questions.CheckboxOption;
import br.elsa.survey.domain.questions.CheckboxQuestion;
import br.elsa.survey.domain.questions.QuestionsFactory;
import br.elsa.survey.services.QuestionService;
import br.elsa.survey.view.beans.QuestionEditorHandler;
import br.elsa.survey.view.messages.ViewMessages;

@ViewScoped
@ManagedBean
public class CheckboxQuestionEditionView {

	@Inject
	private QuestionEditorHandler questionEditorHandler;
	
	private CheckboxQuestion question;
	
	@Inject
	protected QuestionService questionService;

	@Inject
	protected QuestionsFactory questionsFactory;

	/**
	 * Component used to put messages into view context.
	 */
	@Inject
	protected ViewMessages viewMessages;
	
	@PostConstruct
	public void postConstruct() {
		this.question = (CheckboxQuestion) questionEditorHandler.getQuestion();
	}
	
	/**
	 * Add a new {@link CheckboxOption} to the
	 * {@link CheckboxQuestionCreationView#newQuestion} instance.
	 */
	public void addNewOption() {

		question.createNewEmptyOption();
	}

	/**
	 * Removes the last created {@link CheckboxOption} from the
	 * {@link CheckboxQuestionCreationView#newQuestion} instance.
	 */
	public void removeLastChoice() {

		if (question.getOptions().size() > 1) {
			question.removeLastOption();
		}
	}

	/**
	 * Aditional input validation before creating the question TODO study a way
	 * of put this validation logic in a separated component, may be an
	 * interceptor...
	 */
	public void editQuestion() {

		if (question.getMinNumberChosenOptions() <= question.getOptions().size()) {
			System.out.println("### editing...");

		} else {
			viewMessages.addErrorMessage("The number of MIN chosen options can not be greater than the options cout.");
		}
	}

	public CheckboxQuestion getQuestion() {
		return question;
	}

	public void setQuestion(CheckboxQuestion question) {
		this.question = question;
	}
	
	
	
}
